package hust.ioic.oa.view.action;

import hust.ioic.oa.base.BaseAction;
import hust.ioic.oa.domain.Department;
import hust.ioic.oa.domain.Role;
import hust.ioic.oa.utils.DepartmentUtils;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class DepartmentAction extends BaseAction<Department> {

	private Integer parentId;

	/**
	 * 列表
	 * 
	 * @return
	 */
	public String list() {
		List<Department> departmentList = null;
		if (parentId == null) {
			departmentList = departmentService.findTopList();
		} else {
			departmentList = departmentService.findChildren(parentId);
			Department parent = departmentService.getById(parentId);
			ActionContext.getContext().put("parent", parent);
		}
		ActionContext.getContext().put("departmentList", departmentList);
		return "list";
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delete() {
		departmentService.delete(model.getId());

		return "toList";
	}

	/**
	 * 更改页面
	 * 
	 * @return
	 */
	public String editUI() {
		Department department = departmentService.getById(model.getId());
		List<String> dpartNames = new ArrayList<String>();
		List<Department> dpartList = departmentService.findAll();
		for (Department d : dpartList) {
			if (d != null && model.getId() != d.getId()) {
				dpartNames.add(d.getName());
			}
		}
		ActionContext.getContext().put("dpartNames", dpartNames);

		List<Department> topList = departmentService.findTopList();
		List<Department> departmentList = DepartmentUtils
				.getAllDepartments(topList);
		ActionContext.getContext().put("departmentList", departmentList);

		// 准备回显的数据

		ActionContext.getContext().getValueStack().push(department);
		if (department.getParent() != null) {
			parentId = department.getParent().getId();
		}
		return "saveUI";
	}

	/**
	 * 更改
	 * 
	 * @return
	 */
	public String edit() {
		Department department = departmentService.getById(model.getId());
		Department parent = departmentService.getById(parentId);
		// 2，设置要修改的属性
		department.setName(model.getName());
		department.setRemark(model.getRemark());
		department.setParent(parent); // 设置所属的上级部门

		// 3，更新到数据库中
		departmentService.update(department);
		return "toList";
	}

	/**
	 * 添加页面
	 * 
	 * @return
	 */
	public String addUI() {
		List<String> dpartNames = new ArrayList<String>();
		List<Department> dpartList = departmentService.findAll();
		for (Department d : dpartList) {
			if (d != null) {
				dpartNames.add(d.getName());
			}
		}
		ActionContext.getContext().put("dpartNames", dpartNames);
		List<Department> topList = departmentService.findTopList();
		List<Department> departmentList = DepartmentUtils
				.getAllDepartments(topList);
		ActionContext.getContext().put("departmentList", departmentList);
		return "saveUI";
	}

	/**
	 * 添加
	 * 
	 * @return
	 */
	public String add() {
		Department parent = departmentService.getById(parentId);
		model.setParent(parent);
		departmentService.save(model);
		return "toList";
	}

	// ====================Getter and Setter==================================

	public Integer getParentId() {
		return parentId;
	}

	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}
}
